<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="120px" @submit.native.prevent>
      <el-form-item label="单位名称" prop="departmentId" class="dislabel">
        <el-cascader v-model="pram.departmentId" :disabled="pram.departmentId ===0" :options="parentOptions" :props="categoryProps" style="width:100%" />
      </el-form-item>
      <el-form-item label="车牌号" prop="carNumber">
        <el-input v-model="pram.carNumber" maxlength="100" placeholder="车牌号" />
      </el-form-item>
      <el-form-item label="驾驶员" prop="driver">
        <el-input v-model="pram.driver" maxlength="100" placeholder="驾驶员" />
      </el-form-item>
      <el-form-item label="准驾车型" prop="accurate">
        <el-input v-model="pram.accurate" maxlength="100" placeholder="准驾车型" />
      </el-form-item>
      <el-form-item label="车型" prop="vehicleModel">
        <el-input v-model="pram.vehicleModel" maxlength="100" placeholder="车型" />
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input v-model="pram.phone" maxlength="100" placeholder="联系方式" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="pram.remark" maxlength="100" placeholder="备注" />
      </el-form-item>
      <el-form-item label="序号">
        <el-input-number v-model="pram.number" :min="0"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')" v-hasPermi="['admin:privateCar:update','admin:privateCar:save']">{{ isCreate===0?'确定':'更新' }}</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import * as roleApi from '@/api/role.js'
import * as carApi from '@/api/privateCar.js'
import {Debounce} from '@/utils/validate'
export default {
  // name: "edit"
  components: { },
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
    allTreeList:{
      type: Array
    },
  },
  data() {
    return {
      constants:this.$constants,
      pram: {
        departmentId:null,
        driver:'',
        accurate:'',
        vehicleModel:'',
        carNumber:'',
        phone:'',
        remark:'',
        number:0,
        id: 0,
      },
      rules: {
        departmentId: [{ required: true, message: '请选择单位', trigger: ['blur', 'change'] }],
        carNumber: [{ required: true, message: '请填车牌号', trigger: ['blur', 'change'] }],
        driver: [{ required: true, message: '请填驾驶员', trigger: ['blur', 'change'] }],
        accurate: [{ required: true, message: '请填准驾车型', trigger: ['blur', 'change'] }],
        vehicleModel: [{ required: true, message: '请填车型', trigger: ['blur', 'change'] }],
        phone: [{ required: true, message: '请填联系方式', trigger: ['blur', 'change'] }],
      },
      parentOptions: [],
      categoryProps: {
        value: 'id',
        label: 'name',
        children: 'child',
        expandTrigger: 'hover',
        checkStrictly: true,
        emitPath: false
      },
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },
    initEditData() {
      this.parentOptions = [...this.allTreeList]

      if (this.isCreate !== 1) return //编辑
      const { departmentId, driver, accurate, vehicleModel,carNumber,
        phone,remark,id ,number } = this.editData //通过list中slot-scope=“scope“，scope.$index就是该行的下标，scope.row就是该行的数据所有消息对象，有了这两个参数就可以实现编辑、删除功能（this.editData就是scope.row传入的对象参数）
      this.pram.departmentId = departmentId
      this.pram.driver = driver
      this.pram.accurate = accurate
      this.pram.vehicleModel = vehicleModel
      this.pram.carNumber = carNumber
      this.pram.phone = phone
      this.pram.remark = remark
      this.pram.id = id
      this.pram.number = number
    },

    handlerSubmit:Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) return
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    handlerSave() {
      carApi.typeAdd(this.pram).then(data => {
        this.$message.success('创建成功')
        this.$emit('hideEditDialog')
      })
    },
    handlerEdit() {
      carApi.typeAdd(this.pram).then(data => {
        this.$message.success('更新成功')
        this.$emit('hideEditDialog')
      })
    },
  }
}
</script>

<style scoped>

</style>
